import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_jd/pages/tabbar/cart/controller.dart';
import 'package:flutter_jd/ui/pages/cart/checkbox.dart';
import 'package:flutter_jd/ui/widgets/kit.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CartGoodsItem extends GetWidget<CartController> {
  final int cIndex;
  final int index;

  CartGoodsItem(this.index, this.cIndex);

  @override
  Widget build(BuildContext context) {
    var item = controller.productList[index]['children'][cIndex];

    return Container(
      child: Row(
        children: [
          _buildItemCheckBox(index, cIndex, item),
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                GestureDetector(
                  onTap: () {
                    controller.toDetail(item);
                  },
                  child: Container(
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(5.r),
                      child: Image.asset(item['src']),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(left: 10.w),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        GestureDetector(
                          onTap: () {
                            controller.toDetail(item);
                          },
                          child: Container(
                            margin: EdgeInsets.only(top: 5.h),
                            child: Text(
                              item['name'],
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                color: Color(0xFF333333),
                                fontSize: 13.sp,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 7.h),
                          padding: EdgeInsets.fromLTRB(5.w, 4.h, 5.w, 4.h),
                          decoration: BoxDecoration(
                            color: Color(0xFFF6F6F6),
                            borderRadius: BorderRadius.circular(
                              10.r,
                            ),
                          ),
                          child: Text(
                            '白色，128GB，官方标配，选服务',
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              color: Color(0xFF777777),
                              fontSize: 11.sp,
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 10.h),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Row(
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  Text(
                                    '¥',
                                    style: TextStyle(
                                      color: Colors.red,
                                      fontWeight: FontWeight.w500,
                                    ),
                                  ),
                                  Text(
                                    item['price'].toString(),
                                    style: TextStyle(
                                      color: Colors.red,
                                      fontSize: 18.sp,
                                      fontWeight: FontWeight.w500,
                                    ),
                                  ),
                                  Text(
                                    '.00',
                                    style: TextStyle(
                                      color: Colors.red,
                                      fontWeight: FontWeight.w500,
                                    ),
                                  )
                                ],
                              ),
                              Container(
                                height: 25.h,
                                alignment: Alignment.center,
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    GestureDetector(
                                      onTap: () {
                                        if (item['num'] > 1) {
                                          controller.productList[index]
                                                  ['children'][cIndex]['num'] =
                                              --item['num'];
                                        } else {
                                          Kit.showToast(text: '最少购买1件哦！');
                                        }
                                      },
                                      child: Container(
                                        padding: EdgeInsets.only(
                                          left: 12.w,
                                          right: 10.w,
                                        ),
                                        color: Colors.transparent,
                                        alignment: Alignment.center,
                                        child: Kit.iconFont(
                                          0xe715,
                                          item['num'] == 1
                                              ? Color(0xFF999999)
                                              : Color(0xFF333333),
                                          12.sp,
                                        ),
                                      ),
                                    ),
                                    Container(
                                      width: 40.w,
                                      height: 20.h,
                                      alignment: Alignment.center,
                                      decoration: BoxDecoration(
                                        color: Color(0xFFF5F5F5),
                                        borderRadius: BorderRadius.circular(
                                          3.r,
                                        ),
                                      ),
                                      child: Text(
                                        item['num'].toString(),
                                        style: TextStyle(
                                          fontSize: 12.sp,
                                          fontWeight: FontWeight.w500,
                                        ),
                                      ),
                                    ),
                                    GestureDetector(
                                      onTap: () {
                                        controller.productList[index]
                                                ['children'][cIndex]['num'] =
                                            ++item['num'];
                                      },
                                      child: Container(
                                        padding: EdgeInsets.only(
                                          left: 10.w,
                                          right: 8.w,
                                        ),
                                        color: Colors.transparent,
                                        alignment: Alignment.center,
                                        child: Kit.iconFont(
                                          0xe716,
                                          Color(0xFF333333),
                                          12.sp,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              )
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // 商品勾选
  _buildItemCheckBox(int index, int cIndex, Map item) {
    return GestureDetector(
      onTap: () {
        controller.goodsClick(item, index, cIndex);
      },
      child: Container(
        width: 30.w,
        color: Colors.transparent,
        alignment: Alignment.center,
        child: CartCheckbox(
          item['isSelected'],
        ),
      ),
    );
  }
}
